<template>
    <div>
        <!-- 面包屑导航区 -->
        <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>求职者管理</el-breadcrumb-item>
        <el-breadcrumb-item>求职者列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图 -->
        <el-card class="box-card">
            <!-- 搜索与添加区域 -->
        <el-row :gutter="20">
            <el-col :span="8">
                <el-input placeholder="用户名/姓名"  
                class="input-with-select"
                v-model="queryInfo.querystring" clearable
                @clear="getYonghulist">
                <el-button slot="append" icon="el-icon-search" @click="getYonghulist"></el-button>
                </el-input>
            </el-col>
            <el-col :span="4">
                <el-button type="primary" @click="addDialogVisible=true">添加用户</el-button>
            </el-col>
        </el-row>
        <!-- 用户列表区域 -->
        <el-table
            :data="yonghulist"
             border stripe>
            <el-table-column
                type="index"
                label="#">
            </el-table-column>
            <el-table-column
                prop="username"
                sortable
                label="用户名">
            </el-table-column>
            <el-table-column
                prop="name"
                sortable
                label="姓名">
            </el-table-column>
            <el-table-column
                prop="sex"
                label="性别">
                <template slot-scope="scope">
                    <a v-if="scope.row.sex == '1'">男</a>
                    <a v-if="scope.row.sex == '2'">女</a>
                </template>
            </el-table-column>
            <el-table-column
                prop="email"
                sortable
                label="邮箱">
            </el-table-column>
            <el-table-column
                prop="mobile"
                sortable
                label="电话">
            </el-table-column>
            <el-table-column
                label="操作" width="180px">
                <template slot-scope="scope">
                    <!-- 修改按钮 -->
                    <el-button type="primary" icon="el-icon-edit" 
                    size="mini" @click="showEditDialog(scope.row.id)"></el-button>
                    <!-- 删除按钮-->
                    <el-button type="danger" icon="el-icon-delete" size="mini" 
                    @click="removeUserById(scope.row.id)"></el-button>
                    

                </template>
            </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[1, 2, 5, 10]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
        </el-card>
        <!-- 添加用户的对话框 -->
        <el-dialog
        title="添加用户" :visible.sync="addDialogVisible"
        width="50%" @close="addDialogClosed">
        <!-- 内容主体区 -->
            <el-form :model="addForm" :rules="addFormRules" 
            ref="addFormRef" label-width="70px"  >
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="addForm.username"></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="addForm.name"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-radio v-model="addForm.sex" label="1">男</el-radio>
                    <el-radio v-model="addForm.sex" label="2">女</el-radio>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="addForm.password" show-password></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addForm.email"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mobile">
                    <el-input v-model="addForm.mobile"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addYonghu">确 定</el-button>
            </span>
        </el-dialog>
        <!--修改用户的对话框  -->
        <el-dialog
        title="修改用户"
        :visible.sync="editDialogVisible"
        width="50%" @close="editDialogClosed">
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="editForm.username"></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="editForm.name"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-radio v-model="editForm.sex" label="1">男</el-radio>
                    <el-radio v-model="editForm.sex" label="2">女</el-radio>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="editForm.email"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mobile">
                    <el-input v-model="editForm.mobile"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editYonghuInfo">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data(){
        //验证邮箱的规则
        var checkEmail=(rule,value,cb)=>{
            // 验证邮箱的正则表达式
            const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
            if (regEmail.test(value)) {
                // 合法的邮箱
                return cb()
            }
            cb(new Error('请输入合法的邮箱'))
        }
        // 验证手机号的规则
        var checkMobile = (rule, value, cb) => {
            // 验证手机号的正则表达式
            const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

            if (regMobile.test(value)) {
                return cb()
            }

            cb(new Error('请输入合法的手机号'))
        }
        return {
            //获取用户列表的参数
            queryInfo:{
                querystring:'',
                pagenum:1,
                pagesize:2
            },
            yonghulist:[],
            total:0,
            //控制添加用户对话框的显示和隐藏
            addDialogVisible:false,
            //添加用户的表单数据
            addForm:{
                username:'',
                name:'',
                sex:'1',
                password:'',
                email:'',
                mobile:'',
            },
            //添加表单的验证规则对象
            addFormRules:{
                username:[
                    {required:true,message:'请输入用户名',trigger:'blur'},
                    {min:3,max:10,message:'用户名的长度在3~10之间',trigger:'blur'}
                ],
                name:[
                    {required:true,message:'请输入用户名',trigger:'blur'},
                    {min:3,max:10,message:'用户名的长度在3~10之间',trigger:'blur'}
                ],
                password:[
                    {required:true,message:'请输入密码',trigger:'blur'},
                    {min:6,max:15,message:'密码的长度在6~15之间',trigger:'blur'}
                ],
                email:[
                    {required:true,message:'请输入邮箱',trigger:'blur'},
                    {validator:checkEmail,trigger:'blur'}
                ],
                mobile:[
                    {required:true,message:'请输入手机',trigger:'blur'},
                    {validator:checkMobile,trigger:'blur'}

                ]
            },
            //控制修改用户对话框的显示与隐藏
            editDialogVisible:false,
            //查询到的用户信息对象
            editForm:{},
            //修改表单的验证规则
            editFormRules:{
                email:[
                    {required:true,message:'请输入邮箱',trigger:'blur'},
                    {validator:checkEmail,trigger:'blur'}
                ],
                mobile:[
                    {required:true,message:'请输入手机',trigger:'blur'},
                    {validator:checkMobile,trigger:'blur'}

                ]
            },
        }
    },
    created(){
        this.getYonghulist()
    },
    methods:{
        async getYonghulist(){
        const {data:res} =await this.$http.get('yonghu',{params:this.queryInfo})
        if(res.meta.status!=200) return this.$message.error(res.meta.msg)
        this.yonghulist=res.data.records
        this.total=res.data.total
        },
        //监听pagesize改变的事件
        handleSizeChange(newSize){
            console.log(newSize);
            this.queryInfo.pagesize=newSize
            this.getYonghulist()
        },
        //监听页码值改变的数据
        handleCurrentChange(newPage){
            //console.log(newPage);
            this.queryInfo.pagenum=newPage
            this.getYonghulist()

        },
        //监听添加用户对话框的关闭事件
        addDialogClosed(){
            this.$refs.addFormRef.resetFields()

        },
        //点击按钮，添加新用户
        addYonghu(){
            this.$refs.addFormRef.validate(async valid=>{
                if (!valid) {
                    return
                }
                const {data:res} =await this.$http.post('regyonghu',this.addForm)
                if(res.meta.status!=201){
                    this.$message.error(res.meta.msg)
                }
                this.$message.success(res.meta.msg)
                //隐藏
                this.addDialogVisible=false
                this.getYonghulist()
            })

        },

        //展示用户编辑的对话框
        async showEditDialog(id){
            //console.log(id);
            const {data:res}=await this.$http.get('yonghu/'+id)
            if(res.meta.status!=200){
                return this.$message.error('获取信息错误')
            }
            this.editForm=res.data
            this.editDialogVisible=true
        },
        editDialogClosed(){
            this.$refs.editFormRef.resetFields()
        },
        //修改用户信息
        editYonghuInfo(){
            this.$refs.editFormRef.validate(async valid=>{
                if(!valid) return
                //发起请求
                const {data:res} =await this.$http.put('yonghu/'+this.editForm.id,this.editForm)
                if(res.meta.status!=200){
                    this.$message.error('修改用户信息失败')
                }
                this.editDialogVisible = false
                this.getYonghulist()
                this.$message.success('修改用户信息成功')
                
            })

        },
        //删除用户
        async removeUserById(id){
          //弹窗
           const confirmResult =await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
          }).catch(err=>err)
          //如果用户确定删除则返回字符串 confirm
          //如果用户取消 则cancel
            if(confirmResult!='confirm'){
                return this.$message.info('已经取消删除')
            }
            console.log('确认删除');
            const {data:res}=await this.$http.delete('yonghu/'+id)
            if(res.meta.status!=200){
                return this.$message.error('删除用户失败')
            }
            this.$message.success('删除用户成功')
            this.getYonghulist()
          
        }
        
    }
    
}
</script>

<style lang="less" scoped>

</style>